package com.cmb.service.dto;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.cmb.dto.ParamColumnInfo;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

import static com.cmb.constant.JavaFileConstant.*;

/**
 * @author wyp867322
 * @version 1.0
 * @date 2024/8/7 21:55
 */
@Slf4j
public class AnalyseParamRowListener implements ReadListener<ParamColumnInfo> {

    private String currentKey;

    private Map<String, List<ParamColumnInfo>> paramMap = new HashMap<>();

    @Override
    public void invoke(ParamColumnInfo paramColumnInfo, AnalysisContext analysisContext) {
        String ensColumnName = paramColumnInfo.getEnsColumnName();
        if (ensColumnName.equals("END")) {
            currentKey = null;
            return;
        }
        //从第三行开始读取名称
        String zhColumnName = paramColumnInfo.getZhColumnName();

        if ("输入参数".equals(ensColumnName) || "输出参数".equals(ensColumnName)) {
            paramMap.putIfAbsent(zhColumnName, new ArrayList<>());
            currentKey = zhColumnName;
            return;
        }
        if ("字段英文名称".equals(ensColumnName) || "接口名称".equals(ensColumnName) || "调用地址".equals(ensColumnName)) {
            return;
        }
        paramMap.get(currentKey).add(paramColumnInfo);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        // 输出文件
        if (paramMap.isEmpty()) {
            log.info("没有解析到文件");
        }
        Set<String> clazzNameSet = paramMap.keySet();
        for (String key1 : clazzNameSet) {
            // 获取文件工作路径
            String property = System.getProperty("user.dir");
            String filePath = property + "/src/main/resources" + "/" + key1 + ".java";

            List<ParamColumnInfo> paramColumnInfos = paramMap.get(key1);
            boolean flag = true;
            try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(filePath))) {
                if (flag) {
                    bufferedWriter.write(createFirstRow(key1));
                    bufferedWriter.newLine();
                    bufferedWriter.newLine();
                    flag = !flag;
                }
                // 遍历每一个字段的详细信息
                for (ParamColumnInfo paramColumnInfo : paramColumnInfos) {

                    bufferedWriter.write(createColumnDesc(paramColumnInfo.getZhColumnName()));
                    if ("Y".equals(paramColumnInfo.getNeed())) {
                        bufferedWriter.write(createNotBlank());
                    }
                    if ("varchar2".equals(paramColumnInfo.getColumnType())) {
                        bufferedWriter.write(createLengthAnnotation(Integer.valueOf(paramColumnInfo.getColumnLength()),
                                "Y".equals(paramColumnInfo.getContainsZh())));
                        bufferedWriter.newLine();
                    }
                    // 数字长度拼接
                    if ("number".equals(paramColumnInfo.getColumnType())) {
                        bufferedWriter.write(createNumberAnnotation(paramColumnInfo.getColumnLength()));
                        bufferedWriter.newLine();
                    }
                    // 正则校验

                    String columnName = "String".equals(paramColumnInfo.getColumnType()) ?
                            createStringProperty(paramColumnInfo.getEnsColumnName()) : createNumberProperty(paramColumnInfo.getEnsColumnName());
                    bufferedWriter.write(columnName);
                    // 写入注释

                    bufferedWriter.newLine();
                    bufferedWriter.newLine();
                }
                bufferedWriter.write(LAST_ROW);
            } catch (IOException e) {
                log.error("创建文件出问题{}", e.getMessage());
            }
        }
    }
}
